package com.ruoyi.web.zangchat.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.text.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.zangchat.domain.ChatAccount;
import com.ruoyi.web.zangchat.service.IChatAccountService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户账户Controller
 * 
 * @author wudi
 * @date 2020-07-19
 */
@Controller
@RequestMapping("/zangchat/account")
public class ChatAccountController extends BaseController
{
    private String prefix = "zangchat/account";

    @Autowired
    private IChatAccountService chatAccountService;

    @RequiresPermissions("zangchat:account:view")
    @GetMapping()
    public String account()
    {
        return prefix + "/account";
    }

    /**
     * 查询用户账户列表
     */
    @RequiresPermissions("zangchat:account:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String,Object> map)
    {
        startPage();
        return getDataTable(chatAccountService.accountList(map));
    }

    /**
     * 导出用户账户列表
     */
    @RequiresPermissions("zangchat:account:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChatAccount chatAccount)
    {
        List<ChatAccount> list = chatAccountService.list(new QueryWrapper<>());
        ExcelUtil<ChatAccount> util = new ExcelUtil<ChatAccount>(ChatAccount.class);
        return util.exportExcel(list, "account");
    }

    /**
     * 新增用户账户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户账户
     */
    @RequiresPermissions("zangchat:account:add")
    @Log(title = "用户账户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChatAccount chatAccount)
    {
        return toAjax(chatAccountService.save(chatAccount));
    }

    /**
     * 修改用户账户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ChatAccount chatAccount = chatAccountService.getById(id);
        mmap.put("chatAccount", chatAccount);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户账户
     */
    @RequiresPermissions("zangchat:account:edit")
    @Log(title = "用户账户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChatAccount chatAccount)
    {
        return toAjax(chatAccountService.updateById(chatAccount));
    }

    /**
     * 删除用户账户
     */
    @RequiresPermissions("zangchat:account:remove")
    @Log(title = "用户账户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(chatAccountService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
